/* Copyright (c) 2004-2008, Andriy Nikolov
 * All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.processmining.email;

import javax.swing.*;
import java.awt.Point;
import java.util.LinkedList;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.FontMetrics;
import java.awt.Graphics;

/**
 * <p>A base class for all node implementations in all diagrams in the standalone version
 *
 * @author Andriy Nikolov
 * @version 1.0
 */
public class DiagramNode
    extends JButton {
  Point center;
  int preferredWidth;
  DiagramPanel parentPanel;
  Point offset;
  public LinkedList arcsFrom;
  public LinkedList arcsTo;

  public final static int NODE_WIDTH = 80;
  public final static int NODE_HEIGHT = 24;
  public static int scale = 1;
  public static final int NODE_MIN_INTERVAL = 12;

  public DiagramNode(String name, DiagramPanel parentPanel) {
    super(name);

    this.parentPanel = parentPanel;
    FontMetrics fontMetrics;
    arcsFrom = new LinkedList();
    arcsTo = new LinkedList();
    fontMetrics = parentPanel.getFontMetrics(parentPanel.getFont());
    preferredWidth = NODE_WIDTH;
    if (fontMetrics.stringWidth(name) + 40 * scale > NODE_WIDTH * scale) {
      preferredWidth = fontMetrics.stringWidth(name) + 40 * scale;
    }
    setPreferredSize(new Dimension(preferredWidth * scale, NODE_HEIGHT * scale));
    setBackground(Color.orange);
    addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        startCarry(e);
      }

      public void mouseReleased(MouseEvent e) {
        stopCarry(e);
      }
    });
  }

  public void setParentPanel(DiagramPanel parentPanel) {
    this.parentPanel = parentPanel;
  }

  public void setCenter(Point center) {
    this.center = center;
    setBounds(center.x - preferredWidth * scale / 2,
              center.y - NODE_HEIGHT * scale / 2, preferredWidth * scale,
              NODE_HEIGHT * scale);
  }

  public Point getCenter() {
    return center;
  }

  public void startCarry(MouseEvent e) {
    offset = e.getPoint();
    parentPanel.setNodeBeingCarried(this);
//    System.out.println("startCarry: " + e.getX() + ":" + e.getY());
  }

  public void carry(MouseEvent e) {
    setCenter(e.getPoint());
    repaint();
//    System.out.println("carry: " + e.getX() + ":" + e.getY());
  }

  public void stopCarry(MouseEvent e) {
//    System.out.println("stopCarry: " + e.getX() + ":" + e.getY());
    setCenter(new Point(getCenter().x + e.getX() - offset.x,
                        getCenter().y + e.getY() - offset.y));
    parentPanel.setNodeBeingCarried(null);
    parentPanel.repaint();
  }
}

